package lock_thread;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/9
 * Time:11:48
 * 证明sleep()/wait()会不会释放锁
 **/
public class Demo9 {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    System.out.println("线程1 有了锁lock，进入休眠状态");
                    try {
                        lock.wait(999999 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    /*try {
                        Thread.sleep(3 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程1：执行完成，释放锁 lock");*/
                }
            }
        });
        t1.start();

        Thread.sleep(1000);
        //主线程试图获取锁
        synchronized (lock) {
            System.out.println("主线程获取到锁lock");
        }
    }
}
